﻿using Common;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DMTroube_ZeKer.View
{
    public partial class GiftPage : Form
    {
        public GiftPage()
        {
            InitializeComponent();
            Banding();

        }

        private async void Banding()
        {
            dataGridView1.DataSource = Main.lwlist;
            HttpClient httpClient = new HttpClient();
            foreach (var item in Main.lwlist)
            {
                if (item.Image == null)
                {
                    var response = await httpClient.GetAsync(item.src);
                    // 确保请求成功  
                    response.EnsureSuccessStatusCode();
                    // 读取响应流为字节数组  
                    byte[] imageBytes = await response.Content.ReadAsByteArrayAsync();
                    // 使用MemoryStream将字节数组转换为流  
                    using (MemoryStream ms = new MemoryStream(imageBytes))
                    {
                        // 使用Image.FromStream方法从流中加载图片  
                        Image image = Image.FromStream(ms);
                        item.Image = image;
                    }
                }
            }
        }

        /// <summary>
        /// 礼物列表
        /// </summary>
        public class LW
        {
            public Image Image { get; set; }
            public string src { get; set; }
            public string price { get; set; }
            public string name { get; set; }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            LW newLw = new LW();
            if (Main.lwlist != null)
                Main.lwlist.Insert(0, newLw);
            dataGridView1.Invalidate();
            dataGridView1.Update();
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                // 获取第一行（如果用户只选择了一行）  
                DataGridViewRow selectedRow = dataGridView1.SelectedRows[0];
                if (selectedRow?.Cells == null)
                {
                    return;
                }
                string name = selectedRow.Cells["name"].Value.ToString();
                string price = selectedRow.Cells["price"].Value.ToString();
                string src = selectedRow.Cells["src"].Value.ToString();
                if (Main.lwlist != null)
                {
                    var seletData = Main.lwlist.Where(n => n.name == name && n.price == price && n.src == src).FirstOrDefault();
                    if (seletData != null)
                    {
                        Main.lwlist.Remove(seletData);
                        dataGridView1.Invalidate();
                    }
                }
            }
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            string absolutePath = System.IO.Path.GetFullPath("./Resources/Data/lw.json");
            var SaveData = (List<LW>)dataGridView1.DataSource;
            if (SaveData != null)
            {
                JsonHelper.WriteJsonFile(absolutePath, JsonConvert.SerializeObject(SaveData));
                MessageBox.Show("保存成功");
            }
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {

        }
    }
}
